<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="back-button" @tap="goBack">
						<text class="back-icon">←</text>
					</view>
					<view class="header-info">
						<text class="tool-icon">🌟</text>
						<text class="tool-title">今日运势</text>
						<text class="tool-subtitle">专业的运势分析和吉凶指导</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">个人信息</text>
						
						<view class="form-item">
							<text class="form-label">生日</text>
							<picker 
								mode="date" 
								:value="params.birthday" 
								@change="onBirthdayChange"
								class="date-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayBirthday }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">性别</text>
							<view class="gender-selector">
								<view 
									class="gender-option" 
									:class="{ active: params.gender === '男' }"
									@tap="selectGender('男')"
								>
									<text class="gender-text">男</text>
								</view>
								<view 
									class="gender-option" 
									:class="{ active: params.gender === '女' }"
									@tap="selectGender('女')"
								>
									<text class="gender-text">女</text>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<text class="form-label">查询日期</text>
							<picker 
								mode="date" 
								:value="params.date" 
								@change="onDateChange"
								class="date-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayDate }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '分析中...' : '开始分析'" 
						type="primary"
						@tap="queryFortune"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="fortuneResult">
					<view class="result-section">
						<text class="section-title">综合运势</text>
						
						<view class="overall-fortune">
							<view class="fortune-score">
								<view class="score-circle">
									<text class="score-number">{{ fortuneResult.overall.score }}</text>
									<text class="score-total">/100</text>
								</view>
								<text class="score-level">{{ fortuneResult.overall.level }}</text>
							</view>
							<view class="fortune-desc">
								<text class="desc-text">{{ fortuneResult.overall.description }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 各项运势 -->
				<app-card v-if="fortuneResult">
					<view class="fortune-details">
						<text class="section-title">运势详情</text>
						
						<view class="fortune-items">
							<view class="fortune-item">
								<view class="item-header">
									<text class="item-icon">💼</text>
									<text class="item-title">事业运</text>
									<text class="item-score">{{ fortuneResult.career.score }}分</text>
								</view>
								<text class="item-desc">{{ fortuneResult.career.description }}</text>
							</view>
							
							<view class="fortune-item">
								<view class="item-header">
									<text class="item-icon">💕</text>
									<text class="item-title">爱情运</text>
									<text class="item-score">{{ fortuneResult.love.score }}分</text>
								</view>
								<text class="item-desc">{{ fortuneResult.love.description }}</text>
							</view>
							
							<view class="fortune-item">
								<view class="item-header">
									<text class="item-icon">❤️</text>
									<text class="item-title">健康运</text>
									<text class="item-score">{{ fortuneResult.health.score }}分</text>
								</view>
								<text class="item-desc">{{ fortuneResult.health.description }}</text>
							</view>
							
							<view class="fortune-item">
								<view class="item-header">
									<text class="item-icon">💰</text>
									<text class="item-title">财运</text>
									<text class="item-score">{{ fortuneResult.wealth.score }}分</text>
								</view>
								<text class="item-desc">{{ fortuneResult.wealth.description }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 幸运元素 -->
				<app-card v-if="fortuneResult && fortuneResult.luckyItems">
					<view class="lucky-section">
						<text class="section-title">今日幸运</text>
						
						<view class="lucky-grid">
							<view class="lucky-item">
								<text class="lucky-label">幸运方位</text>
								<text class="lucky-value">{{ fortuneResult.luckyItems.direction }}</text>
							</view>
							<view class="lucky-item">
								<text class="lucky-label">幸运色彩</text>
								<text class="lucky-value">{{ fortuneResult.luckyItems.color }}</text>
							</view>
							<view class="lucky-item">
								<text class="lucky-label">幸运数字</text>
								<text class="lucky-value">{{ fortuneResult.luckyItems.number }}</text>
							</view>
							<view class="lucky-item">
								<text class="lucky-label">幸运时辰</text>
								<text class="lucky-value">{{ fortuneResult.luckyItems.time }}</text>
							</view>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'

export default {
	name: 'FortuneToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				birthday: '',
				gender: '',
				date: ''
			},
			fortuneResult: null,
			loading: false,
			toolsService: null
		}
	},
	computed: {
		canQuery() {
			return this.params.birthday && this.params.gender
		},
		
		displayBirthday() {
			return this.params.birthday || '请选择生日'
		},
		
		displayDate() {
			return this.params.date || '今天'
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
			// 设置默认日期为今天
			const today = new Date()
			this.params.date = today.toISOString().split('T')[0]
		},
		
		goBack() {
			uni.navigateBack()
		},
		
		onBirthdayChange(e) {
			this.params.birthday = e.detail.value
		},
		
		onDateChange(e) {
			this.params.date = e.detail.value
		},
		
		selectGender(gender) {
			this.params.gender = gender
		},
		
		async queryFortune() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请完善个人信息',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const result = await this.toolsService.getFortuneInfo(this.params)
				
				if (result.success) {
					this.fortuneResult = result.data
					uni.showToast({
						title: '分析完成',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '分析失败')
				}
			} catch (error) {
				console.error('运势分析失败:', error)
				uni.showToast({
					title: '分析失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.back-button {
		width: 80rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 40rpx;
		margin-right: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
		
		.back-icon {
			font-size: 32rpx;
			color: #000000;
			font-weight: bold;
		}
	}
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
			margin-bottom: 8rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
			margin-bottom: 8rpx;
		}
		
		.tool-subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.input-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.form-item {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.date-picker {
			.picker-display {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 88rpx;
				padding: 0 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				
				.picker-text {
					font-size: 28rpx;
					color: #000000;
				}
				
				.picker-arrow {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
				}
			}
		}
		
		.gender-selector {
			display: flex;
			gap: 16rpx;
			
			.gender-option {
				flex: 1;
				height: 88rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				transition: all 0.3s ease;
				
				&.active {
					border-color: #F5A623;
					background: linear-gradient(135deg, #F5A623 0%, #E68B00 100%);
					
					.gender-text {
						color: white;
						font-weight: 500;
					}
				}
				
				.gender-text {
					font-size: 28rpx;
					color: #000000;
					transition: color 0.3s ease;
				}
			}
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.result-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.overall-fortune {
		display: flex;
		align-items: center;
		padding: 32rpx;
		background: linear-gradient(135deg, #F5A623 0%, #E68B00 100%);
		border-radius: 16rpx;
		
		.fortune-score {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-right: 32rpx;
			
			.score-circle {
				width: 120rpx;
				height: 120rpx;
				border-radius: 60rpx;
				background-color: rgba(255, 255, 255, 0.2);
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				margin-bottom: 16rpx;
				
				.score-number {
					font-size: 36rpx;
					font-weight: bold;
					color: white;
					line-height: 1;
				}
				
				.score-total {
					font-size: 20rpx;
					color: rgba(255, 255, 255, 0.8);
				}
			}
			
			.score-level {
				font-size: 28rpx;
				font-weight: 500;
				color: white;
			}
		}
		
		.fortune-desc {
			flex: 1;
			
			.desc-text {
				font-size: 28rpx;
				color: white;
				line-height: 1.6;
			}
		}
	}
}

.fortune-details {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.fortune-items {
		.fortune-item {
			padding: 24rpx;
			margin-bottom: 20rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			border-left: 6rpx solid #F5A623;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.item-header {
				display: flex;
				align-items: center;
				margin-bottom: 12rpx;
				
				.item-icon {
					font-size: 32rpx;
					margin-right: 12rpx;
				}
				
				.item-title {
					flex: 1;
					font-size: 28rpx;
					font-weight: 500;
					color: #000000;
				}
				
				.item-score {
					font-size: 24rpx;
					color: #F5A623;
					font-weight: bold;
				}
			}
			
			.item-desc {
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.8);
				line-height: 1.5;
			}
		}
	}
}

.lucky-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.lucky-grid {
		display: grid;
		grid-template-columns: 1fr 1fr;
		gap: 16rpx;
		
		.lucky-item {
			padding: 24rpx;
			background: linear-gradient(135deg, #E8F5E8 0%, #D4F4D4 100%);
			border-radius: 12rpx;
			text-align: center;
			
			.lucky-label {
				display: block;
				font-size: 24rpx;
				color: rgba(0, 0, 0, 0.7);
				margin-bottom: 8rpx;
			}
			
			.lucky-value {
				display: block;
				font-size: 28rpx;
				font-weight: 500;
				color: #2e7d2e;
			}
		}
	}
}
</style>